﻿define(["jquery", "jqueryValidate", "formComponents", "jquerySlimscroll", "bootstrapHoverDropdown", "app"], function ($) {
    var FormValidation = function () {

        var handleValidation1 = function () {
            // for more info visit the official plugin documentation: 
            // http://docs.jquery.com/Plugins/Validation

            //每个js可能不同
            var form1 = $('#appcreate');

            //固定内容
            var error1 = $('.alert-danger', form1).eq(0); //獲取第一個錯誤信息
            var success1 = $('.alert-success', form1);

            form1.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    Identity: {
                        minlength: 4,
                        required: true,
                    },
                    Name: {
                        required: true,
                    },

                },
                messages: {
                    Identity: {
                        required: '請輸入內容',
                        minlength: '最少輸入4個字符'
                    },
                    Name: {
                        required: '請輸入內容',
                    }
                },

                //提交form表單验证失败时
                invalidHandler: function (event, validator) { //display error alert on form submit              
                    success1.hide();
                    error1.show();
                    App.scrollTo(error1, -200);
                },

                //单个input验证失败时
                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                //單個input驗證通過時，后调用该方法
                unhighlight: function (element) { // revert the change done by hightlight
                    $(element)
                        .closest('.form-group').removeClass('has-error'); // set error class to the control group
                },

                //單個input驗證通過時，先调用该方法
                success: function (label) {
                    label
                        .closest('.form-group').removeClass('has-error'); // set success class to the control group                
                },
                submitHandler: function (form) {
                    $("[type='submit']", form).attr("disabled", true).val('提交中..')
                    form.submit();
                }

                //只要定義了submitHandler的話，就不會提交form表單
                //submitHandler: function (form) {

                //}
            });

        }

        var handleWysihtml5 = function () {
            //if (!jQuery().wysihtml5) {

            //    return;
            //}

            //if ($('.wysihtml5').size() > 0) {
            //    $('.wysihtml5').wysihtml5({
            //        "stylesheets": [wysiwyg_color_Src]
            //    });
            //}
        }

        return {
            //main function to initiate the module
            init: function () {

                handleWysihtml5();
                handleValidation1();
            }

        };

    }();

    $(function () {
        FormValidation.init();
        FormComponents.init();
    })
});